const cloud = require('wx-server-sdk');

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
const _ = db.command

exports.add = async (event, context) => {
    await db.collection("shop").add({
        data: {
            _openid: context.OPENID,
            info: "",
            title: "计算机网络",
            value: 20.10,
            img_url: ["https://image.jovie.top/img/202203281141269.jpg"],
            createTime: new Date(), // 创建时间
            updateTime: new Date(),
        }
    })
    try {
        return {
            success: true,
            data: "event.data"
        };
    } catch (e) {
        return {
            success: false,
            errMsg: e
        };
    }
}

exports.get = async (event, context) =>{
    return await db.collection("shop").orderBy("createTime", "desc").get()
}

exports.getOne = async (event, context) =>{
    return await db.collection("shop").where({
        _id: _.eq(event._id)
    }).get()
}